package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.OrderDetailTiny;
import com.xmy.cultivate.entity.views.RecordCourseStudent;
import com.xmy.cultivate.entity.views.RecordCourseTiny;
import com.xmy.cultivate.exception.CommonException;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.ICourseSchedulingDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 排课详情 服务实现类
 * </p>
 *
 * @author min
 * @since 2022-08-01
 */
@Service
public class CourseSchedulingDetailServiceImpl extends ServiceImpl<CourseSchedulingDetailMapper, CourseSchedulingDetail> implements ICourseSchedulingDetailService {

    @Autowired
    CourseSchedulingDetailMapper courseSchedulingDetailMapper;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    StaffMapper staffMapper;

    public CourseSchedulingDetail getOneById(Long id){
        return courseSchedulingDetailMapper.getOneById(id);
    }

    /**
     *
     * @param wrapper
     * @param schedulingId 排课详情id
     * @return
     */
    public List<RecordCourseStudent> recordStudentList(Wrapper wrapper,Long schedulingId,Long gradeId,Long courseId,Long schoolId){

        List<RecordCourseStudent> recordCourseStudentList = courseSchedulingDetailMapper.recordStudentList(wrapper);
        for (RecordCourseStudent recordCourseStudent:recordCourseStudentList){
            //System.out.print("schedulingId:"+schedulingId);
            //System.out.print("recordCourseStudent.getId():"+recordCourseStudent.getId());



            //获取已记上课数据，由于不需要修改，这里暂时注释
            recordCourseStudent.setRecordCourseTiny(new RecordCourseTiny());
            /*RecordCourseTiny recordCourseTiny = recordCourseMapper.getListForSchedulingIdAndStuId(schedulingId,recordCourseStudent.getId());
            if(recordCourseTiny == null){
                recordCourseStudent.setRecordCourseTiny(new RecordCourseTiny());
            }else {
                recordCourseStudent.setRecordCourseTiny(recordCourseTiny);
            }*/
            //recordCourseStudent.setRecordCourseTiny(new RecordCourseTiny());

            Course course = courseMapper.selectById(courseId);
            if(course == null){
                throw new CommonException(ResultCode.COURSE_NOT);
            }

            //剩余课时
            //OrderDetailTiny orderDetailTiny = orderDetailMapper.getResidueCourseCountForCourseId(recordCourseStudent.getId(),courseId,schoolId);
            OrderDetailTiny orderDetailTiny = orderDetailMapper.getResidueCourseCountForSubjectsId(recordCourseStudent.getId(),course.getSubjectsId(),schoolId);

            recordCourseStudent.setOrderDetailTiny(orderDetailTiny);
        }
        return recordCourseStudentList;
    }

    public List<CourseSchedulingDetail> findAll(Wrapper wrapper){
        List<CourseSchedulingDetail> courseSchedulingIPage = courseSchedulingDetailMapper.findAll(wrapper);
        return courseSchedulingIPage;
    }

    public IPage<CourseSchedulingDetail> findAllForPage(IPage<?> page,Wrapper wrapper){
        IPage<CourseSchedulingDetail> courseSchedulingIPage = courseSchedulingDetailMapper.findAllForPage(page,wrapper);
        return courseSchedulingIPage;
    }

    /**
     * 排课日程表
     * @param page
     * @param wrapper
     * @return
     */
    public IPage<CourseSchedulingDetail> calendarList(IPage<CourseSchedulingDetail> page,Wrapper wrapper){
        IPage<CourseSchedulingDetail> courseSchedulingDetailIPage = courseSchedulingDetailMapper.selectPage(page,wrapper);
        for (CourseSchedulingDetail courseSchedulingDetail:courseSchedulingDetailIPage.getRecords()){

        }
        return courseSchedulingDetailIPage;
    }


    /**
     * 查找冲突排课
     * @param wrapper
     * @return
     */
    public List<CourseSchedulingDetail> getFindClash(Wrapper wrapper){
        List<CourseSchedulingDetail> courseSchedulingDetailList = courseSchedulingDetailMapper.getFindClash(wrapper);

        List<CourseSchedulingDetail> courseSchedulingDetailListResult = new ArrayList<>();
        for(CourseSchedulingDetail courseSchedulingDetail:courseSchedulingDetailList){

            QueryWrapper<CourseSchedulingDetail> courseSchedulingDetailQueryWrapper = new QueryWrapper<>();
            courseSchedulingDetailQueryWrapper.eq("school_id",courseSchedulingDetail.getSchoolId());
            courseSchedulingDetailQueryWrapper.eq("grade_id",courseSchedulingDetail.getGradeId());
            courseSchedulingDetailQueryWrapper.eq("school_date",courseSchedulingDetail.getSchoolDate());
            courseSchedulingDetailQueryWrapper.eq("time_start",courseSchedulingDetail.getTimeStart());
            courseSchedulingDetailQueryWrapper.eq("time_end",courseSchedulingDetail.getTimeEnd());

            List<CourseSchedulingDetail> courseSchedulingDetailList2 = courseSchedulingDetailMapper.selectList(courseSchedulingDetailQueryWrapper);
            if(courseSchedulingDetailList2.size()>1){
                for(CourseSchedulingDetail courseSchedulingDetail2:courseSchedulingDetailList2){
                    Grade grade = gradeMapper.selectById(courseSchedulingDetail2.getGradeId());
                    if(grade != null){
                        courseSchedulingDetail.setGradeName(grade.getName());
                        Staff staff = staffMapper.selectById(grade.getTeacherId());
                        if(staff != null){
                            courseSchedulingDetail.setTeacherName(staff.getName());
                        }
                    }
                    courseSchedulingDetailListResult.add(courseSchedulingDetail2);
                }
            }
        }
        return courseSchedulingDetailListResult;
    }

    /**
     * 获取排课与调课、补课、消课信息
     * @param queryWrapper
     * @return
     */
    public List<RepairCourse> getSchedulingUnionRepair(QueryWrapper queryWrapper){
        List<RepairCourse> repairCourseList = courseSchedulingDetailMapper.getSchedulingUnionRepair(queryWrapper);
        return repairCourseList;
    }

    public CourseSchedulingDetail getCourseSchedulingDetailById(Long id){
        return courseSchedulingDetailMapper.getCourseSchedulingDetailById(id);
    }

    @Override
    public List<CourseSchedulingDetail> getCourseSchedulingDetailList(Wrapper wrapper){
        return courseSchedulingDetailMapper.getCourseSchedulingDetailList(wrapper);
    }
}
